/**
 * 
 */
package org.myontology.controller.impl;

import model.Vocabulary;

import org.myontology.controller.AbstractAction;
import org.myontology.controller.Action;
import org.myontology.controller.AttributeHolder;
import org.myontology.controller.AttributeLevel;
import org.myontology.controller.SesameSession;
import org.myontology.controller.ValueHandler;
import org.myontology.controller.rules.SimpleRuleEngine;
import org.myontology.controller.rules.impl.Actions;
import org.myontology.logging.ElementLogging;
import org.openrdf.model.URI;

import controller.sesameDataConnector.AbstractDataConnector;

/**
 *  @author <a href="michael.hackl@sti2.at">Michael Hackl</a>
 * Project: Myontology - www.myontology.org
 * STI
 */
public class ShowAction extends AbstractAction implements Action {

	private SimpleRuleEngine rules;
	private SesameSession session;
	private ElementLogging elementLogging;
	
	public static final ValueHandler<URI> URI = new ValueHandler<URI>(AttributeLevel.SESSION,ShowAction.class);
	public static final ValueHandler<Boolean> WRITEABLE = new ValueHandler<Boolean>(AttributeLevel.REQUEST,ShowAction.class);

	
	/* (non-Javadoc)
	 * @see org.myontology.controller.Action#execute(org.myontology.controller.AttributeHolder)
	 */
	public void execute(AttributeHolder attributes) throws Exception {
		URI uri;
		if (attributes.getRequestAttribute("action").equals("lock")) {
			uri = URI.getValue(attributes);
			if (session.hasTriple(uri, Vocabulary.ISLOCKED, uri)) {
				session.remove(uri, Vocabulary.ISLOCKED, uri);
			}
			else {
				session.add(uri, Vocabulary.ISLOCKED, uri);
			}
		}
		else {
			uri= AbstractDataConnector.getURI( attributes.getRequestParameter("id") );
			URI.setValue(attributes, uri);
			
			
		}
		
		elementLogging.logView(uri,attributes);
		
		if (rules.check(Actions.EDIT, attributes,session)) {
			WRITEABLE.setValue(attributes, true);
		}
		else {
			WRITEABLE.setValue(attributes, false);
		}
	}

}
